//
//  enum HMRequestMethod: String {     case GET = "GET"     case POST = "POST" BGNetworkTools.swift
//  Baga
//
//  Created by 飞王 on 16/12/29.
//  Copyright © 2016年 飞王. All rights reserved.
//

import UIKit


/// 请求方法
///
/// - GET:  get
/// - POST: post
enum BGRequestMethod: String {
    case GET = "GET"
    case POST = "POST"
}

class BGNetworkTools: AFHTTPSessionManager {
    
    // 单例
    static let sharedTools: BGNetworkTools = {
        let instance = BGNetworkTools()
        instance.responseSerializer.acceptableContentTypes?.insert("text/html")
        instance.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return instance
    }()
    
    // (response: AnyObject?, error: NSError?)->()
    // 定义请求完成的回调的别名
    typealias BGRequestCallBack = (_ response: AnyObject?, _ error: NSError?)->()
    
    
//    - (NSURLSessionDataTask *)GET:(NSString *)URLString
//    parameters:(id)parameters
//    success:(void (^)(NSURLSessionDataTask *task, id responseObject))success
//    failure:(void (^)(NSURLSessionDataTask *task, NSError *error))failure
//    {
    
//    typealias BGSuccessBlock = (_ task:URLSessionDataTask, _ responseObject:Any)-> Void
//    
//    typealias BGFailureBlock = (_ task:URLSessionDataTask?, _ error: NSError?)-> Void
//    
//    func request(method: BGRequestMethod = .GET, urlString: String,parameters: AnyObject?, success: BGSuccessBlock, failure: BGFailureBlock) {
//        if method == .GET {
//            GET(urlString, parameters: parameters, progress: nil, success: BGSuccessBlock, failure: BGFailureBlock)
//        }
//    }
//    
    /// 请求数据
    ///
    /// - parameter urlString:  请求地址
    /// - parameter parameters: 请求参数
    /// - parameter finished:   请求成功或者失败的回调
    func request(method: BGRequestMethod = .GET, urlString: String, parameters: AnyObject?, finished:  @escaping BGRequestCallBack){
        
        // dataTaskWithHttp 是写在 .m 文件里面的
        // 对应在 Swift 中的，就是 private 修饰的方法
        
        // 定义请求成功的闭包
        let success = { (dataTask: URLSessionDataTask, responseObject: Any?) -> Void in
            finished(responseObject as AnyObject?, nil)
        }
        
        // 定义请求失败的闭包
        let failure = { (dataTask: URLSessionDataTask?, error: Error) -> Void in
            finished(nil, error as NSError?)
        }
        
        if method == .GET {
            
            get(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }else{
            post(urlString, parameters: parameters, progress: nil, success: success, failure: failure)
        }
    }
    
    
    /// 发送请求(上传文件)
    ///
    /// - parameter data:       <#data description#>
    /// - parameter name:       <#name description#>
    /// - parameter urlString:  <#urlString description#>
    /// - parameter parameters: <#parameters description#>
    /// - parameter finished:   <#finished description#>
//    func requestWithData(data: NSData, name: String, urlString: String, parameters: AnyObject?, finished:HMRequestCallBack) {
//        // 定义请求成功的闭包
//        let success = { (dataTask: NSURLSessionDataTask, responseObject: AnyObject?) -> Void in
//            finished(response: responseObject, error: nil)
//        }
//        
//        // 定义请求失败的闭包
//        let failure = { (dataTask: NSURLSessionDataTask?, error: NSError) -> Void in
//            finished(response: nil, error: error)
//        }
//        
//        POST(urlString, parameters: parameters, constructingBodyWithBlock: { (formData) -> Void in
//            formData.appendPartWithFileData(data, name: name, fileName: "aa", mimeType: "application/octet-stream")
//            }, progress: nil, success: success, failure: failure)
//    }


}

//MARk: - 封装AFN 网络方法
//extension BGNetworkTools {
//    
//    func request(method: BGRequestMethod, Urlstring: String,parameters: [String:AnyObject]?, finished:(_ result: AnyObject?,_ error: NSError?)->()){
//        
//        if method == BGRequestMethod.GET {
//            GET(Urlstring, parameters: parameters, success: { (_, result) -> Void in
//                
//                finished(result: result, error: nil)
//                
//            }) { (_, error) -> Void in
//                
//                //在开发网络应用的时候，错误虽然不要提示给用户，但是一定要输出，我们程序猿一定要知道
//                print(error)
//                finished(result: nil, error: error)
//            }
//        }else{
//            
//            POST(Urlstring, parameters: parameters, success: { (_, result) -> Void in
//                
//                finished(result: result, error: nil)
//                
//            }) { (_, error) -> Void in
//                
//                //在开发网络应用的时候，错误虽然不要提示给用户，但是一定要输出，我们程序猿一定要知道
//                print(error)
//                finished(result: nil, error: error)
//            }
//            
//        }
//        
//    }
//    
//    
//}


